using System.Collections.Generic;
using IFramework;
using System;

namespace IFramework
{
    public class UIManager : Singleton<UIManager>, IUIManager
    {
        private Stack<UIBase> UIStack;
        private Stack<Type> TypeStack;
        public int StackCount { get { return UIStack.Count; } }
        public override void OnSingletonInit()
        {
            UIStack = new Stack<UIBase>();
            TypeStack = new Stack<Type>();
        }
        public bool Contains(UIBase ui)
        {
            return UIStack.Contains(ui);
        }
        public bool Contains(Type uiType)
        {
            return TypeStack.Contains(uiType);
        }
        public bool Contains<T>()
        {
            return Contains(typeof(T));
        }
        public void BackTo<T>(params object[] paras) where T : UIBase
        {
            if (TypeStack.Contains(typeof (T)))
            {
                while (TypeStack.Peek()!=typeof(T))
                {
                    Pop();
                }
            }
        }
        public UIBase Peek()
        {
            if (UIStack.Count == 0) return null;
            return UIStack.Peek();
        }
        public Type PeekType()
        {
            return TypeStack.Peek();
        }
        public void Pop(params object[] paras)
        {
            if (UIStack .Count !=0)
            {
                PoolManager.Instance.Restore(TypeStack.Peek(), UIStack.Peek(), this, paras);
                UIStack.Pop();
                TypeStack.Pop();
            }
            if (UIStack.Count != 0)
            {
                PoolManager.Instance.GetInitObj(UIStack.Peek(), this, paras);
            }
        }
        public void Push<T>(T Prefab, params object[] paras)where T:UIBase
        {
            if (UIStack.Count != 0)
            {
                PoolManager.Instance.Restore(TypeStack.Peek(), UIStack.Peek(), this, paras);
            }
            PoolManager.Instance.GetInitObj(Prefab, this, paras);
            TypeStack.Push(Prefab.GetType());
        }

    }
}